#include "ServerReaderThread.h"
#include "Socket.h"

ServerReaderThread::ServerReaderThread(Socket* s, int i)
{
	m_Socket=s;
	m_Id=i;
};
void ServerReaderThread::Work()
{
	
	while(true)
	{	
		SockMessage outMsg;
		
			SockMessage inMsg;
			
			std::string s;
			char who[20];
			if(m_Socket->Recv(&inMsg))
			{
				
				inMsg.ReadString(&s);
				
				sprintf(who,"Client %d:",m_Id);
				std::cout<<who<<s.c_str()<<"\n";

				
				outMsg.WriteString(who);
				outMsg.WriteString(s);
			}
		

		//check if outmsg is anything
		if(!outMsg.IsEmpty())
		{
			for(unsigned int i=0;i<TheServer::Instance()->NumOfClients();++i)
			{
				TheServer::Instance()->GetClient(i)->Send(outMsg);
			}
		}

	}
}